import common.constants.ProjectPath;
import dto.ConfDto;
import dto.ConfigDto;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import service.ConfigService;
import service.DataBaseService;
import service.GenCodeService;
import utils.FileUtils;
import utils.DBUtils;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/**
 * Created by cxland on 2017/9/27.
 */
public class MainClass {
    public static void main(String[] args)  throws Exception {
        String path = null;
        if (args.length==0){
            path= "./conf.yaml";
            System.out.println("默认配置");
        }else {
            path = args[0];
        }
        //createCode(path);
        DataBaseService.setConf();
    }

    public static void createCode(String filePath) throws Exception{
        //   BufferedReader br   = new BufferedReader(new InputStreamReader(System.in ));
        System.out.println("=====================*代码生成器*====================");
        if (ConfigService.findFile(filePath)){

            String pjPath = ConfigService.projectPath();
            Map<String,String> dbConf = ConfigService.dbConf();
            String[] tableConf = ConfigService.ruleConf();

            ConfDto dto =new ConfDto();
            dto.setDbConf(dbConf);
            dto.setIgnoreTable(tableConf);

            List<String> list = DBUtils.getNeedGenerateTable(dto);
            Scanner input;
            Scanner packageInput;
            String table = "";
            String path = "";
            String packageName = "";
            String testPath ="";
            System.out.println();
            System.out.println("#请输入表名：");
            while (true){
                input = new Scanner(System.in);
                try {
                    table = input.next();
                    boolean is = true;
                    for(String str :list){
                        if (table.equals(str)){
                            path = pjPath;
                            String  packageConf = ConfigService.getPackageName(table);
                            if(packageConf ==null){
                                System.out.println();
                                System.out.println("#该表模块包名未指定，请手动输入：");
                                packageInput = new Scanner(System.in);
                                packageName = packageInput.next()+packageInput.nextLine().trim();
                              //  FileUtils.readFile(filePath,table,packageName);
                            }else {
                                packageName = packageConf;
                            }
                            is = false;
                        }
                    }
                    if (is){
                        throw new Exception();
                    }else {
                        testPath = path+ ProjectPath.MG_WORLD+packageName;
                        if (!FileUtils.testFileExist(testPath)){
                            GenCodeService.generateDDL(table, path, packageName);
                            GenCodeService.generateService(table, path, packageName);
                            GenCodeService.generateDao(table, path, packageName);
                            System.out.println();
                            System.out.println("#操作成功，代码地址："+path);

                        }else {
                            System.out.println();
                            System.out.println("包名重复,该表已存在业务代码，请选择其他数据表！");
                        }
                    }
                } catch(Exception e) {
                    System.out.println();
                    System.out.println("输入错误,请重新输入：");
                    continue;
                }
                break;
            }
        }
        System.out.println();
        System.out.println("======================代码生成器====================");
    }



}
